<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Controller;
use App\Models\Tokens;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class AuthAuthorization extends Controller
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if ($request->header('Authorization') == null) {
            return $this->returnUnauthenticatedError();
        }
        $object = Tokens::where("token", $request->header('Authorization'))->first();
        if ($object == null) {
            return $this->returnUnauthenticatedError();
        }
        $request->object_id = $object->object_id;
        $request->object_role = $object->role;
        return $next($request);
    }
}
